<?php
/** CreateLecture class
 *
 * Command line script
 * Usage:
 *		php createLecture.php parentPageId univisKey semester
 * Example:
 *		php createLecture.php 11 Lecture.tech.IMMD.IMMD5.gdi 2007w
 * 
 * Provides methods to create Typo3 pages and Filemounts for a lecture
 * 
 */

/*
if ($argc != 4 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>
	This PHP command line script creates Typo3 pages and Filemounts for a lecture.

	Usage:
		php <?php echo $argv[0]; ?> parentPageId univisKey semester

	Options:
		- parentPageId 
		- univisKey has to be a valid lecture key like Lecture.tech.IMMD.IMMD5.gdi
		- semester (e.g. 2007s or 2007w) 

	The --help, -help, -h, or -? options display this help message.
<?php
	exit();
}
*/

require_once (t3lib_extMgm::extPath('univis2typo3').'/mod1/configuration/include/common.php');
require_once (t3lib_extMgm::extPath('univis2typo3').'/mod1/configuration/include/class.u2t3_SysMountPoint.php');
require_once (t3lib_extMgm::extPath('univis2typo3').'/mod1/configuration/include/class.u2t3_BEGroup.php');

/*
# Execute
$cl = new CreateLecture();
$cl->setParentPageId($argv[1]);
$cl->setLectureKey($argv[2]);
$cl->setSemester($argv[3]);
$cl->create();
*/

class CreateLecture {
	var $parentPageId_ = '';
	var $lectureKey_ = '';
	var $semester_ = '';
	var $name_ = '';
	var $short_ = '';
	var $gid_ = '';

	function setParentPageId($parentPageId) { $this->parentPageId_ = $parentPageId; }
	function setLectureKey($lectureKey) { $this->lectureKey_ = $lectureKey; }
	function setSemester($semester) { $this->semester_ = $semester; }

	function create() {
		if (!$this->parentPageId_ || !$this->lectureKey_ || !$this->semester_) {
			echo "\t error: parentPageId or lecture key or semester not specified.\n";
			return -1;
		}

		$lecture =& $this->loadLectureData();

		$sysMount = new u2t3_SysMountPoint();
		$beGroup  = new u2t3_BEGroup();

		$sysMount->setFolder('lectures/'.$this->semester_.'/'.$this->short_);
		$sysMount->setName('Lecture.'.$this->semester_.'.'.$this->short_);
		$sysMountId = $sysMount->create();

		$beGroup->setName('Lecture.'.$this->semester_.'.'.$this->short_);
		$this->gid_ = $beGroup->create();
		$lectureId = $this->createPages();

		$beGroup->setDbMountpoints  ($lectureId);
		$beGroup->setFileMountpoints($sysMountId);
	}

	function loadLectureData() {
		$query = "SELECT DISTINCT semester, name, short FROM `u_lecture`"
				. " WHERE `key` =  '{$this->lectureKey_}' AND semester = '{$this->semester_}'";

		$rows = performSQLQuery($query);
		$row =& $rows[0];
		
		$this->name_ = $row['name'];
		$this->short_ = $row['short'];

		return $row;
	}

	function createPages() {
		$now = time();
		$name = $this->name_.' ['.$this->short_.']';
		
		$query = "INSERT INTO `pages` ( `uid`, `pid`, `t3ver_oid`, `t3ver_id`, `t3ver_wsid`, `t3ver_label`,
				`t3ver_state`, `t3ver_stage`, `t3ver_count`, `t3ver_tstamp`, `t3ver_swapmode`,
				`t3_origuid`, `tstamp`, `sorting`, `deleted`, `perms_userid`, `perms_groupid`,
				`perms_user`, `perms_group`, `perms_everybody`, `editlock`, `crdate`, `cruser_id`,
				`title`, `doktype`, `TSconfig`, `storage_pid`, `is_siteroot`, `php_tree_stop`,
				`tx_impexp_origuid`, `url`, `hidden`, `starttime`, `endtime`, `urltype`,
				`shortcut`, `shortcut_mode`, `no_cache`, `fe_group`, `subtitle`, `layout`,
				`target`, `media`, `lastUpdated`, `keywords`, `cache_timeout`, `newUntil`,
				`description`, `no_search`, `SYS_LASTCHANGED`, `abstract`, `module`,
				`extendToSubpages`, `author`, `author_email`, `nav_title`, `nav_hide`,
				`content_from_pid`, `mount_pid`, `mount_pid_ol`, `alias`, `l18n_cfg`,
				`fe_login_mode`)
				VALUES
				( '', {$this->parentPageId_}, 0, 0, 0, '', 0, 0, 0, 0, 0, 0, {$now}, 8, 0, 1,
				{$this->gid_}, 31, 27, 0, 0, {$now}, 1, '{$name}', 1, '', 0, 0, 0, 0, '', 
				0, 0, 0, 1, 0, 0, 0, '', '', 0, '', '', 0, '', 0, 0, '', 0, 0, '', '', 0, '', '',
				'', 0, 0, 0, 0, '', 0, 0);";

		performSQLQuery($query, false);
		$lecturePageId = mysql_insert_id();

		$languageId = 0;
		$pi_flexform = '<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>\n<T3FlexForms>\n <data>\n <sheet index="sDEF">\n <language index="lDEF">\n <field index="what_to_display">\n <value index="vDEF">einzelne Vorlesung</value>\n </field>\n <field index="pages">\n <value index="vDEF"></value>\n </field>\n <field index="list">\n <value index="vDEF"></value>\n </field>\n </language>\n <language index="lDE">\n <field index="what_to_display">\n <value index="vDEF">einzelne Vorlesung</value>\n </field>\n <field index="pages">\n <value index="vDEF"></value>\n </field>\n <field index="list">\n <value index="vDEF"></value>\n </field>\n </language>\n </sheet>\n </data>\n</T3FlexForms>';
		$l18n_diffsource = '0';
		
		$query = "INSERT INTO `tt_content` (`uid`, `pid`, `t3ver_oid`, `t3ver_id`, `t3ver_wsid`, `t3ver_label`, `t3ver_state`,
				`t3ver_stage`, `t3ver_count`, `t3ver_tstamp`, `t3_origuid`, `tstamp`, `hidden`, `sorting`, `CType`, `header`,
				`header_position`, `bodytext`, `image`, `imagewidth`, `imageorient`, `imagecaption`, `imagecols`, `imageborder`,
				`media`, `layout`, `deleted`, `cols`, `records`, `pages`, `starttime`, `endtime`, `colPos`, `subheader`, `spaceBefore`,
				`spaceAfter`, `fe_group`, `header_link`, `imagecaption_position`, `image_link`, `image_zoom`, `image_noRows`,
				`image_effects`, `image_compression`, `altText`, `titleText`, `longdescURL`, `header_layout`, `text_align`, `text_face`,
				`text_size`, `text_color`, `text_properties`, `menu_type`, `list_type`, `table_border`, `table_cellspacing`,
				`table_cellpadding`, `table_bgColor`, `select_key`, `sectionIndex`, `linkToTop`, `filelink_size`, `section_frame`, `date`,
				`splash_layout`, `multimedia`, `image_frames`, `recursive`, `imageheight`, `rte_enabled`, `sys_language_uid`, `tx_impexp_origuid`,
				`pi_flexform`, `l18n_parent`, `l18n_diffsource`)  VALUES
				('', {$lecturePageId}, 0, 0, 0, '', 0, 0, 0, 0, 0, {$now}, 0, 8, 'list', '', '', '', '', 0, 8, '', 0, 0, '', 0, 0, 0, '', '',
				0, 0, 0, '', 0, 0, '', '', '', '', 0, 0, 0, 0, '', '', '', '0', '', 0, 0, 0, 0, '0', 'univis2web_pi1', 0, 0, 0, 0, '', 1, 0, 0,
				0, 0, '0', '', 0, 0, 0, 0, {$languageId}, 0,
				'{$pi_flexform}',
				0, {$l18n_diffsource});";

		performSQLQuery($query, false);

		return $lecturePageId;
	}
}
?>
